/*
 * @Author: M_xiaohan
 * @Date: 2018-05-01 15:51:31
 * @Last Modified by: M_xiaohan
 * @Last Modified time: 2018-05-19 15:53:45
 */
import { getInternalTradeTypeDetailList } from '../services/internaltradetype';
import { addCar } from '../services/car';

export default {
  namespace: 'internaltradetypeDetail',

  state: {
    internaltradetypeDetailList: [],
    addSuccess: false,
    carData: [],
  },

  effects: {
    *fetchInternaltradetypeDetalList({ payload }, { call, put }) {
      const response = yield call(getInternalTradeTypeDetailList, payload);
      yield put({
        type: 'getInternalTradeTypeDetailListData',
        payload: {
          ...response.data,
        },
      });
    },
    *addToCar({ payload }, { call, put }) {
      const response = yield call(addCar, payload);
      if (response.code === 0) {
        yield put({
          type: 'addSuccess',
          payload,
        });
        yield put({
          type: 'global/getCarAllData',
          payload: {
            customerId: 1,
          },
        });
      } else {
        throw new Error();
      }
    },
    *searchData({ payload }, { call, put }) {
      const response = yield call(getInternalTradeTypeDetailList, payload);
      yield put({
        type: 'getInternalTradeTypeDetailListData',
        payload: response.data,
      });
    },
  },

  reducers: {
    getInternalTradeTypeDetailListData(state, action) {
      let newDataSource = [];
      action.payload.list.map((item) => {
        let hasAddedCar = false;
        state.carData.forEach((element) => {
          if (element.internalTradeId === item.id) {
            hasAddedCar = true;
          }
        });
        if (hasAddedCar) {
          item.hasAddToCar = true;
        } else {
          item.hasAddToCar = false;
        }
        return item;
      });
      if (action.payload.isFirstPage) {
        newDataSource = action.payload.list;
      } else {
        newDataSource = state.internaltradetypeDetailList.concat(action.payload.list);
      }
      return {
        ...state,
        internaltradetypeDetailList: newDataSource,
        hasNextPage: action.payload.hasNextPage,
        nextPage: action.payload.nextPage,
      };
    },
    addSuccess(state, action) {
      const newDataSource = state.internaltradetypeDetailList.map((item) => {
        if (item.id === action.payload.internalTradeId) {
          item.hasAddToCar = true;
        }
        return item;
      });
      return {
        ...state,
        ...action.payload,
        internaltradetypeDetailList: newDataSource,
      };
    },
    getAllCarsData(state, action) {
      console.log(action.payload);
      return {
        ...state,
        carData: action.payload,
      };
    },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen((location) => {
        if (location.pathname.includes('internaltradetypelist')) {
          dispatch({
            type: 'fetchInternaltradetypeDetalList',
            payload: {
              internalType: history.location.query ? history.location.query.internalType : '蔬菜类',
              pageNum: 0,
            },
          });
        }
      });
    },
  },
};
